using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public struct OtherSize
{
    public int w;
    public int h;
}
public class OtherEditor
{
    private string[] m_otherList;
    private OtherSize[] m_otherSizeList;
    private int m_curOther = 0;
    private int m_curRotate = 0;
    private bool[] m_isSingular = {false, false};// 长宽是否单数
    
    private int m_nIndexX = 0;
    private int m_nIndexZ = 0;
    public void Init()
    {
        uint nCount = DBCManager.GetInstance().GetOtherDBC().GetRecordCount( );
        m_otherList = new string[nCount];
        m_otherSizeList = new OtherSize[nCount];
        for(uint i = 0; i < nCount; i++)
        {
            m_otherSizeList[i] = new OtherSize();
            m_otherList[i] = DBCManager.GetInstance().GetOtherDBC().GetString(i, 0);
            m_otherSizeList[i].w = DBCManager.GetInstance().GetOtherDBC().GetInt(i, 2);
            m_otherSizeList[i].h = DBCManager.GetInstance().GetOtherDBC().GetInt(i, 3);
        }
    }

    public void OnGUI()
    {
        int oldCurOther = m_curOther;
        m_curOther = EditorGUILayout.Popup("物件名称", m_curOther, m_otherList);
        if(oldCurOther != m_curOther)
        {
            m_curRotate = 0;
        }

        MyBuildingTool.selObj.transform.localScale = new Vector3(m_otherSizeList[m_curOther].w,0.1f,m_otherSizeList[m_curOther].h);  
        if(m_curRotate == 0 || m_curRotate == 2)      
        {
            m_isSingular[0] = m_otherSizeList[m_curOther].w%2 == 1;
            m_isSingular[1] = m_otherSizeList[m_curOther].h%2 == 1;
        }
        else
        {            
            m_isSingular[0] = m_otherSizeList[m_curOther].h%2 == 1;
            m_isSingular[1] = m_otherSizeList[m_curOther].w%2 == 1;
        }
        MyBuildingTool.selObj.transform.position = new Vector3(m_isSingular[0]?(float)m_nIndexX+0.5f:(float)m_nIndexX+1,0,m_isSingular[1]?(float)m_nIndexZ+0.5f:(float)m_nIndexZ+1);
        MyBuildingTool.selObj.transform.rotation = GridData.s_Quaternion[m_curRotate];
    }

    public void OnSceneGUI(SceneView sceneview)
    {
        int controlID = GUIUtility.GetControlID( FocusType.Passive);
        if(Event.current.type == EventType.Layout)
        {
            HandleUtility.AddDefaultControl(controlID);
        }
        Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
        RaycastHit hitInfo;

        if (Physics.Raycast(worldRay, out hitInfo, 10000.0f, GridManager.mGridListLayerMask))
        {                
            m_nIndexX = m_isSingular[0]?(int)hitInfo.point.x:(int)(hitInfo.point.x-0.5f);
            m_nIndexZ = m_isSingular[1]?(int)hitInfo.point.z:(int)(hitInfo.point.z-0.5f);
            
            if(Event.current.type == EventType.MouseMove || (Event.current.type == EventType.MouseDown && Event.current.button == 0))
            {// 未按键移动 or 点击左键
                MyBuildingTool.selObj.transform.position = new Vector3(m_isSingular[0]?(float)m_nIndexX+0.5f:(float)m_nIndexX+1,0,m_isSingular[1]?(float)m_nIndexZ+0.5f:(float)m_nIndexZ+1);
            }
            else if(Event.current.type == EventType.MouseUp && Event.current.button == 0)
            {
               GridManager.GetInstance().AddBuilding(ObjType.eot_Other, m_otherList[m_curOther], m_nIndexX, m_nIndexZ, m_curRotate);
            }
            else if(Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.R)
            {
                m_curRotate++;
                if(m_curRotate > 3)
                {
                    m_curRotate = 0;
                }
                if(m_curRotate == 0 || m_curRotate == 2)      
                {
                    m_isSingular[0] = m_otherSizeList[m_curOther].w%2 == 1;
                    m_isSingular[1] = m_otherSizeList[m_curOther].h%2 == 1;
                }
                else
                {            
                    m_isSingular[0] = m_otherSizeList[m_curOther].h%2 == 1;
                    m_isSingular[1] = m_otherSizeList[m_curOther].w%2 == 1;
                }
                MyBuildingTool.selObj.transform.position = new Vector3(m_isSingular[0]?(float)m_nIndexX+0.5f:(float)m_nIndexX+1,0,m_isSingular[1]?(float)m_nIndexZ+0.5f:(float)m_nIndexZ+1);
                MyBuildingTool.selObj.transform.rotation = GridData.s_Quaternion[m_curRotate];
            }
        }
    }
}